// pages/exp.js
var utils = require('../../utils/util.js')
var moment = require('../../utils/moment.min.js')
require('../../utils/moment-locale-zh-cn.js')

Page({

    /**
     * 页面的初始数据
     */
    data: {
        loading: true
    },

    attends: [],

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.getDataFromServer()
    },

    getDataFromServer: function (callback) {
        const that = this;
        utils.httpRequest({
            url: '/Experiment/my',
            success: function (res) {
                console.log(res)
                var expList = res.data.expList
                var attendList = res.data.attendList
                var expMap = {}
                for (var i in expList) {
                    var exp = expList[i]
                    var completePercent = (100 * exp.attendCount / 4000).toFixed(1)
                    if (exp.id == "1") {
                        exp.name = '双盲实验（顺序）'
                        exp.desp = '依次对每张图片进行评分, 可多次参与实验' //（仅收集一份数据）
                        exp.imgUrl = '../../images/Love_and_Thanks.jpg'
                        exp.statInfo = exp.attendCount + '人次参与 完成度' + completePercent + '%'
                    } else if (exp.id == "2") {
                        exp.name = '三盲实验（顺序）'
                        exp.desp = '依次对每张图片进行评分, 不限参与次数'
                        exp.imgUrl = '../../images/swan_lake.jpg'
                        exp.statInfo = exp.attendCount + '人次参与 完成度' + completePercent + '%'
                    } else if(exp.id == '3'){
                        exp.name = '双盲&三盲实验（对比）'
                        exp.desp = '从2张图片中挑出最美的一张，不限参与次数'
                        exp.imgUrl = '../../images/You_are_beautiful.jpg'
                        exp.statInfo = exp.attendCount + '人次参与 完成度' + completePercent + '%'
                    } else if (exp.id == '4') {
                        exp.imgUrl = '../../images/3.jpg'
                        exp.statInfo = exp.attendCount + '人次参与 完成度' + completePercent + '%'
                    } else if(exp.id == '5'){
                        exp.imgUrl = '../../images/2.jpg'
                        exp.statInfo = exp.attendCount + '人次参与 完成度' + completePercent + '%'
                    } else if(exp.id=='6'){
                        exp.name = '[游戏]' + exp.name
                        exp.imgUrl = '../../images/psychic-powers.jpg'
                        exp.statInfo = exp.attendCount + '人次参与 点击查看结果'
                    }
                    exp.nonFinish = false
                    expMap[exp.id] = exp
                }
                var attends = attendList
                for (var i in attends) {
                    var attend = attends[i]
                    attend.expName = expMap[attend.expid].name
                    attend.expId = attend.expid
                    if (attend.completed == '0') {
                        expMap[attend.expid].nonFinish = true
                    }
                }

                for (var i in attends) {
                    var attend = attends[i]
                    attend.begin_t = moment(attend.begin_t).fromNow()
                    if (attend.end_t != null) {
                        attend.end_t = moment(attend.end_t).fromNow()
                    } else {
                        attend.end_t = attend.begin_t
                    }
                }
                that.attends = attends

                that.setData({
                    'loading': false,
                    'expList': expList,//.slice(0, 1),
                    'attendList': attends
                })
                if (callback) callback()
            }
        })
    },

    attend: function (event) {
        var expId = event.currentTarget.dataset.expId
        var url;
        console.log(expId)
        for (var i in this.attends) {
            if (this.attends[i].expId == expId) {
                this.gotoExp(expId)
                return
            }
        }
        var that = this
        wx.showModal({
            title: '授权数据收集',
            content: '我们将收集并以非商业目的使用您的评分(如授权其他科研人员使用)。同意并继续请点确定，否则取消',
            success: function (res) {
                if (res.confirm) {
                    that.gotoExp(expId)
                }
            }
        })

    },

    viewDetail: function (event) {
        var expId = event.currentTarget.dataset.expId
        console.log(expId)
        if(expId=='6'){
            wx.navigateTo({ url: 'psi/precognition-result-all' })
        }
    },

    actionGo: function (event) {
        var aid = event.currentTarget.dataset.attendId
        var eid = event.currentTarget.dataset.expId

        for (var i in this.attends) {
            if (this.attends[i].id == aid) {
                if (this.attends[i].completed == "0") {
                    this.gotoExp(eid)
                } else {
                    this.gotoResult(eid, aid)
                }
            }
        }
    },

    gotoExp: function (expId) {
        var url
        switch (parseInt(expId)) {
            case 1: url = 'double/vote'; break;
            case 2: url = 'triple/vote'; break;
            case 3: url = 'selection/vote'; break;
            case 6: url = 'psi/precognition';break;
        }
        if (url) {
            wx.navigateTo({ url: url })
        }
    },

    gotoResult: function (expId, attendId) {
        var url
        switch (parseInt(expId)) {
            case 1: url = 'double/result?attendID=' + attendId; break;
            case 6: url = 'psi/precognition-result'; break;
        }
        if (url) {
            wx.navigateTo({ url: url })
        }
    },

    gotoIndex: function () {
        wx.redirectTo({
            url: '/pages/index/index', complete: function (r) { console.log(r) }
        })
    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {
        this.setData({
            loading: true
        })
        this.getDataFromServer(function () { wx.stopPullDownRefresh() })
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {
        return { title: '意念影响水结晶实验' }
    }
})